#include "renderer.h"
#include <vector>
#include <iterator>
#include <iostream>
renderer::renderer(){
	//Create window of width x and height y
	SDL_CreateWindowAndRenderer(1920, 1080, 0, &_window, &_renderer);

	SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 0);

	SDL_RenderClear(_renderer);
	//Set window title to game name
	SDL_SetWindowTitle(_window, "crosscity-test");
	load_content();
}

/*destructor 	for (std::vector<SDL_Texture *>::iterator i = _textures.begin(); i != _textures.end(); ++i)
		SDL_DestroyTexture(*i);*/

void renderer::addTileToRender(tile a){
	//std::cout << a.get_texture_id() << std::endl;
	SDL_RenderCopy(_renderer, _textures[a.get_texture_id()], NULL, a.get_rect());
}


void renderer::load_content(){
	_textures.push_back(SDL_CreateTextureFromSurface(_renderer, IMG_Load("../content/sprites/tiles/grass.png")));
	_textures.push_back(SDL_CreateTextureFromSurface(_renderer, IMG_Load("../content/sprites/tiles/sea.png")));
	SDL_SetTextureAlphaMod(_textures[0], 255);
}

void renderer::render(map m){
	for (auto& inner_vector : m.get_vector_map()) {
		for (auto& element : inner_vector) {
			addTileToRender(element);
		}
	}

	SDL_RenderPresent(_renderer);
}

renderer::~renderer(){
	SDL_DestroyWindow(_window);
	SDL_Quit();
}
